//
//  AnchorController.m
//  ProductB
//
//  Created by 田阳 on 16/8/5.
//  Copyright © 2016年 田阳. All rights reserved.
//

#import "AnchorController.h"
#import "AnchorListModel.h"
#import "CategoryFirstCell.h"
#import "CategorySecondCell.h"
#import "RecommendSessionView.h"
#import "MoreAnchorController.h"
@interface AnchorController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *collection;
@property(nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

//存放数据解析字典
@property(nonatomic,strong) NSMutableDictionary *firstModelDic;

//存放title数组
@property(nonatomic,strong) NSMutableArray *titleArray;
@property(nonatomic,strong) NSMutableArray *modelArray;
@property(nonatomic,strong) NSMutableArray *idArray;
@property(nonatomic,strong) NSMutableArray *typeArray;
@end

@implementation AnchorController

- (NSMutableArray *)typeArray
{
    if (!_typeArray) {
        self.typeArray = [[NSMutableArray alloc] init];
    }
    return _typeArray;
}

- (NSMutableArray *)idArray
{
    if (!_idArray) {
        self.idArray = [[NSMutableArray alloc] init];
    }
    return _idArray;
}

- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        self.titleArray = [[NSMutableArray alloc] init];
    }
    return _titleArray;
}

- (NSMutableDictionary *)firstModelDic
{
    if (!_firstModelDic) {
        self.firstModelDic = [[NSMutableDictionary alloc] init];
    }
    return _firstModelDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatCollectionView];
    [self dataRequestWithUrl];
}

- (void)creatCollectionView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 40);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 114 - kScreenHeight / 20) collectionViewLayout:_flowLayout];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerClass:[RecommendSessionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"anchorHeaderView"];
    [_collection registerNib:[UINib nibWithNibName:@"CategoryFirstCell" bundle:nil] forCellWithReuseIdentifier:@"CategoryFirstCell"];
    [_collection registerNib:[UINib nibWithNibName:@"CategorySecondCell" bundle:nil] forCellWithReuseIdentifier:@"AnchorSecondCell"];
    [self.view addSubview:self.collection];
}

- (void)dataRequestWithUrl
{
    [NetWorkrequestManage requestWithType:GET url:KAnchorURL parameters:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dic[@"famous"];
        for (NSDictionary *dict in array) {
            NSArray *listArray = dict[@"list"];
            NSMutableArray *modelArray = [[NSMutableArray alloc] init];
            for (NSDictionary *dictionary in listArray) {
                AnchorListModel *model = [[AnchorListModel alloc] init];
                [model setValuesForKeysWithDictionary:dictionary];
                [modelArray addObject:model];
            }
            NSString *str = dict[@"title"];
            NSString *categoryId = dict[@"id"];
            [self.idArray addObject:categoryId];
            [self.titleArray addObject:str];
            [self.firstModelDic setObject:modelArray forKey:str];
            }
        
        NSArray *array1 = dic[@"normal"];
        for (NSDictionary *dict in array1) {
            NSMutableArray *modelArray = [[NSMutableArray alloc] init];
            for (NSDictionary *dictionary in dict[@"list"]) {
                AnchorListModel *model = [[AnchorListModel alloc] init];
                [model setValuesForKeysWithDictionary:dictionary];
                [modelArray addObject:model];
            }
            NSString *str = dict[@"title"];
            [self.titleArray addObject:str];
            NSString *type = dict[@"name"];
            [self.typeArray addObject:type];
            [self.firstModelDic setObject:modelArray forKey:str];
        }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collection reloadData];
            });
    } error:nil];
}

#pragma mark ---collectionFlowyout代理方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return CGSizeMake(kScreenWidth, 90);
    }else{
    return CGSizeMake((kScreenWidth - 60) / 3, (kScreenWidth - 60) / 3 + 60);
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 3) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }
}

#pragma mark ---collectionView代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.firstModelDic.count - 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr = self.firstModelDic[self.titleArray[section]];
    if (arr.count == 4) {
        return 3;
    }
    return arr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    RecommendSessionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"anchorHeaderView" forIndexPath:indexPath];
    headerView.titleLabel.text = self.titleArray[indexPath.section];
    [headerView.button addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    headerView.button.tag = indexPath.section;
    return headerView;
}

- (void)btnAction:(UIButton *)btn{
    NSInteger index = btn.tag;
    MoreAnchorController *moreVC = [[MoreAnchorController alloc] init];
    if (index >= self.idArray.count) {
        moreVC.myIndex = _typeArray[index - 4];
        moreVC.type = 0;
    }else{
        moreVC.myIndex = _idArray[index];
        moreVC.type = 1;
    }
    moreVC.title = _titleArray[index];
    [self.navigationController pushViewController:moreVC animated:YES];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        NSArray *array = self.firstModelDic[self.titleArray[indexPath.section]];
        AnchorListModel *model = array[indexPath.row];
        CategorySecondCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AnchorSecondCell" forIndexPath:indexPath];
        [cell setDataWithModel:model];
        return cell;
    }else{
        NSArray *array = self.firstModelDic[self.titleArray[indexPath.section]];
        AnchorListModel *model = array[indexPath.row];
        CategoryFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryFirstCell" forIndexPath:indexPath];
        [cell setDataWithModel:model];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
}
@end
